﻿using System;
using System.Collections.Generic;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// AlipaySetting服务
    /// </summary>
    public class AlipaySettingService : IService
    {
        private readonly AlipaySettingDao _alipaySettingDao;
		private readonly WorkContext _workContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
		/// <param name="workContext"></param>
        public AlipaySettingService(MySqlDbContext mySqlDbContext, WorkContext workContext)
        {
			_workContext = workContext;
            _alipaySettingDao = new AlipaySettingDao(mySqlDbContext);
        }

        

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<AlipaySettingListDto> PageList(AlipaySettingListQueryDto query)
        {
            var list = _alipaySettingDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<AlipaySetting>, List<AlipaySettingListDto>>(list);
            return new PagedResponse<AlipaySettingListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(AlipaySettingEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<AlipaySettingEditDto, AlipaySetting>(dto);
            entity.SignType = "RSA2";
            entity.Charset = "utf-8";
            entity.CreatedTime=DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _alipaySettingDao.Add(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(AlipaySettingEditDto dto)
        {
            var entity = _alipaySettingDao.GetSingle(1);
            if (entity == null)
            {
                Create(dto);
                return;
            }

            TinyMapperExtensions.Map(dto, entity);

			entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _alipaySettingDao.Update(entity);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AlipaySettingEditDto Info(long id)
        {
            var entity = _alipaySettingDao.GetSingle(1);
            return TinyMapperExtensions.Map<AlipaySetting, AlipaySettingEditDto>(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _alipaySettingDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _alipaySettingDao.Delete(entity);
        }
    }
}
